package org.jeecg.common.view;

import org.apache.commons.csv.CSVPrinter;
import org.jeecg.common.export.CsvExportServer;
import org.jeecgframework.poi.excel.def.MapExcelConstants;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author gchiaway
 * 日期: 2022-04-21
 * 时间: 14:50
 */
@SuppressWarnings("unchecked")
@Controller("mapCsvView")
public class MapCsvView extends MiniAbstractCsvView {

    public MapCsvView() {
        super();
    }

    /**
     * Subclasses must implement this method to actually render the view.
     * <p>The first step will be preparing the request: In the JSP case,
     * this would mean setting model objects as request attributes.
     * The second step will be the actual rendering of the view,
     * for example including the JSP via a RequestDispatcher.
     *
     * @param model    combined output Map (never {@code null}),
     *                 with dynamic values taking precedence over static attributes
     * @param request  current HTTP request
     * @param response current HTTP response
     * @throws Exception if rendering failed
     */
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Expose-Headers", "content-disposition,result-status");
        Boolean success = (Boolean) model.get("success");
        if (!success) {
            String message = (String) model.get("message");
            String code = (String) model.get("code");
            if (isIE(request)) {
                message = java.net.URLEncoder.encode(message, "UTF8");
                code = java.net.URLEncoder.encode(code, "UTF8");
            } else {
                message = new String(message.getBytes("UTF-8"), "ISO-8859-1");
                code = new String(code.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader("result-status", "success=" + false + ";message=" + message + ";code=" + code);
            return;
        }
        String codedFileName = "临时文件";
        String extensionName = CSV;
        if (model.containsKey(MapExcelConstants.FILE_NAME)) {
            codedFileName = (String) model.get(MapExcelConstants.FILE_NAME);
        }
        if (isIE(request)) {
            codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
            extensionName = java.net.URLEncoder.encode(extensionName, "UTF8");
        } else {
            codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
            extensionName = new String(extensionName.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ";extensionname=" + extensionName);
        response.setHeader("result-status", "success=true");
        ServletOutputStream out = response.getOutputStream();

        CSVPrinter csvPrinter = CsvExportServer.exportCsv(out, (List<ExcelExportEntity>) model.get(MapExcelConstants.ENTITY_LIST), (List<Map<String, String>>) model.get(MapExcelConstants.MAP_LIST));
        csvPrinter.flush();
        csvPrinter.close();
    }
}
